<%
Class FastAsp_Debug
    Private m_Enabled    '是否启用调试
    Private m_Console    '是否显示控制台
    Private m_Logs       '调试日志
    Private m_StartTime  '开始时间
    Private m_Memory     '内存使用
    
    Private Sub Class_Initialize()
        m_Enabled = GetConfig("DebugMode")
        m_Console = GetConfig("Console")
        Set m_Logs = Server.CreateObject("Scripting.Dictionary")
        m_StartTime = Timer()
        m_Memory = GetMemoryUsage()
    End Sub
    
    '添加调试信息
    Public Function Log(byval msg, byval type_)
        If Not m_Enabled Then Exit Function
        
        If type_ = "" Then type_ = "info"
        Dim logItem : Set logItem = Server.CreateObject("Scripting.Dictionary")
        logItem.Add "time", Now()
        logItem.Add "type", type_
        logItem.Add "message", msg
        logItem.Add "memory", GetMemoryUsage()
        logItem.Add "runtime", FormatNumber(Timer() - m_StartTime, 4)
        
        m_Logs.Add m_Logs.Count, logItem
        Set Log = Me
    End Function
    
    '输出调试信息
    Public Sub Show()
        If Not m_Enabled Then Exit Sub
        
        If m_Console Then
            ShowConsole()
        Else
            ShowPage()
        End If
    End Sub
    
    '显示控制台
    Private Sub ShowConsole()
        Response.Write "<script type='text/javascript'>"
        Dim key, logItem
        For Each key In m_Logs.Keys
            Set logItem = m_Logs(key)
            Response.Write "console." & logItem("type") & "('[" & logItem("runtime") & "s][" & FormatNumber(logItem("memory")/1024/1024, 2) & "MB] " & _
                Replace(logItem("message"), "'", "\'") & "');" & vbCrLf
        Next
        Response.Write "</script>"
    End Sub
    
    '显示调试页面
    Private Sub ShowPage()
        Response.Write "<style type='text/css'>"
        Response.Write ".debug-panel{position:fixed;bottom:0;left:0;right:0;background:#fff;border-top:1px solid #ccc;padding:10px;font-size:12px;z-index:9999;max-height:300px;overflow:auto;}"
        Response.Write ".debug-panel .info{color:#666;}"
        Response.Write ".debug-panel .warn{color:#f90;}"
        Response.Write ".debug-panel .error{color:#f00;}"
        Response.Write "</style>"
        
        Response.Write "<div class='debug-panel'>"
        Response.Write "<div>总运行时间: " & FormatNumber(Timer() - m_StartTime, 4) & "秒</div>"
        Response.Write "<div>内存占用: " & FormatNumber(GetMemoryUsage()/1024/1024, 2) & "MB</div>"
        Response.Write "<div>调试信息:</div>"
        
        Dim key, logItem
        For Each key In m_Logs.Keys
            Set logItem = m_Logs(key)
            Response.Write "<div class='" & logItem("type") & "'>"
            Response.Write "[" & logItem("runtime") & "s][" & FormatNumber(logItem("memory")/1024/1024, 2) & "MB] "
            Response.Write Server.HTMLEncode(logItem("message"))
            Response.Write "</div>"
        Next
        Response.Write "</div>"
    End Sub
    
    '获取内存使用
    Private Function GetMemoryUsage()
        On Error Resume Next
        Dim mem : Set mem = GetObject("winmgmts:root\cimv2")
        Dim procs : Set procs = mem.ExecQuery("Select * from Win32_Process Where ProcessId = " & Request.ServerVariables("PROCESS_ID"))
        Dim proc
        For Each proc In procs
            GetMemoryUsage = proc.WorkingSetSize
            Exit Function
        Next
        GetMemoryUsage = 0
    End Function
End Class
%> 